<template>
	<div>
	  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm">
	    <el-form-item label="捐赠名目" prop="name">
	      <el-input v-model="ruleForm.name"></el-input>
	    </el-form-item>
	    <el-form-item label="捐赠人" prop="goodpeople">
			<el-select style="width: 100%" v-model="ruleForm.goodpeople" filterable placeholder="请选择">
			    <el-option
			      v-for="item in options"
			      :key="item.id"
			      :label="item.name"
			      :value="item.id">
			    </el-option>
			</el-select>
	    </el-form-item>
		<el-form-item label="捐赠金额" prop="money">
		<!--		    <el-input v-model="ruleForm.money"></el-input>-->
		        <el-input-number style="width: 100%" v-model="ruleForm.money" :precision="2" :step="100" ></el-input-number>
				  </el-form-item>
				  <el-form-item label="原始协议文件(可选)" prop="applyUrl">
		        <el-upload
		          class="avatar-uploader"
		          action=""
		          :show-file-list="false"
		          :before-upload="beforeUploads"
		          :http-request="doPut"
		        >
		            <img v-if="ruleForm.applyUrl" :src="ruleForm.applyUrl" class="avatar">
		          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
		        </el-upload>
		</el-form-item>
	    <el-form-item>
	      <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
	      <el-button @click="resetForm('ruleForm')">重置</el-button>
	    </el-form-item>
	  </el-form>
	</div>
</template>

<script>
  import Utils from "../assets/js/Utils";
  export default {
    name:"AddGoodMoney",
    data() {
      return {
        options: [],
        ruleForm: {
          name: '',
          goodpeople: '',
          money: '',
          applyUrl: '',
        },
        rules: {
          name:
            [
              { required: true, message: '不能为空', trigger: 'blur' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
            ],
          goodpeople:
            [
              { required: true, message: '不能为空', trigger: 'blur' },
            ],
          money:
            [
              { required: true, message: '不能为空', trigger: 'blur' },
            ],
          applyUrl:
            [
              //{ required: true, message: '不能为空', trigger: 'blur' },
            ],
          }
      }
    },
    methods: {
      submitForm(formName) {
        console.log(formName);
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let _this = this;
            this.$post({
              url : "income/create",
              data:{
                name : _this.ruleForm.name,
                projectId : Utils.GetQueryString("project_id"),
                goodPeopleId : _this.ruleForm.goodpeople,
                money : _this.ruleForm.money,
                protocolRootUrl : _this.ruleForm.applyUrl
              }
            }).then(res =>{
              alert('提交成功!即将跳转到项目列表');
              this.$router.push({
                path:'project?project_id=' + Utils.GetQueryString("project_id")
              });
            })
          } else {
            alert('提交失败!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      beforeUploads(file){
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      doPut(option){
        let file = option.file
        let OSS = require('ali-oss');
        this.$post({
          url : "sts/sign"
        }).then(res =>{
          let ossClient = new OSS({
              region: 'oss-cn-qingdao',
              accessKeyId: res.accessKeyId,
              accessKeySecret: res.accessKeySecret,
              stsToken: res.securityToken,
              bucket: 'lius-1'
          });
          ossClient.put("imgs/" + (new Date()).getTime() + ".jpg", file).then(res =>{
            this.ruleForm.applyUrl = res.url
          })
        }).catch(err => {
          console.log(err)
        })
      }
    },
    mounted() {
      this.$post({
        url : "goodpeople/read"
      }).then(res =>{
        this.options = res
      })
    }
  }
</script>

<style scoped>
  .avatar-uploader{
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #409EFF;
    border-radius: 6px;
    cursor: pointer;
    position: absolute;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    border: 1px dashed #409EFF;
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
